﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace LearnWPF.Controls.Helper
{
    public class TextBoxHelper
    {
        /// <summary>
        /// 附加属性：IsFocused
        /// </summary>
        public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached("IsFocused",
            typeof(bool), 
            typeof(TextBoxHelper),
            new FrameworkPropertyMetadata(false, OnIsFocusedPropertyChanged));

        public static void SetIsFocused(DependencyObject dp, bool value)
        {
            dp.SetValue(IsFocusedProperty, value);
        }

        public static bool GetIsFocused(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsFocusedProperty);
        }

        private static void OnIsFocusedPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if ((bool)e.NewValue)
            {
                textBox.Focus();
                textBox.SelectAll();
            }
        }
    }
}
